import requests
from lxml import etree

if __name__ == "__main__":
    # ua伪装
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"
    }
    responseText = requests.get("https://bj.58.com/ershoufang/", headers=headers).text
    with open("./file/58.html", "w", encoding="utf-8") as fp:
        fp.write(responseText)
    tree = etree.HTML(responseText)
    fList = tree.xpath("//div[@tongji_tag='fcpc_ersflist_gzcount']")
    for f in fList:
        # 局部解析
        # print(f.xpath(".//div[@class='property-content-title']/h3/text()"))
        print(f.xpath("./a/div[2]/div/div/h3/text()")[0])
